<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>仓库分类管理</title>
<%@ include file="/WEB-INF/pages/common/frameCommonJs.jsp"  %>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/wareHouseClassService.js'></script>
<style type="text/css">
#fm{
	margin:0;
	padding:10px 30px;
}
.ftitle{
	font-size:14px;
	font-weight:bold;
	color:#666;
	padding:5px 0;
	margin-bottom:10px;
	border-bottom:1px solid #ccc;
}
.fitem{
	margin-bottom:5px;
}
.fitem label{
	display:inline-block;
	width:80px;
}
</style>
<script>
   var getClassUrl = "<%=request.getContextPath()%>/warehouse/getAllWareHouseClassJson.action";
   $(function(){
		$('#wcTree').tree({
			animate: true,
			url: getClassUrl,
			onBeforeExpand:function(node){
				$('#wcTree').tree("options").url=getClassUrl + "?wc.id=" + node.id;
			},
			onClick:function(node){
				$('#wcTree').tree('expand',node.target);
			},
			onContextMenu: function(e, node){
				e.preventDefault();
				$('#wcTree').tree('select', node.target);
				$('#mm').menu('show', {
					left: e.pageX,
					top: e.pageY
				});
			}
		});
   });
   var currentParentId = ""; 
   // 新增同级
   function newSamePc(){
	   closeTr();
		var	title = "新增一级分类";
   		$('#dlg').dialog('open').dialog('setTitle',title);  
	    $('#fm').form('clear');  
	    url = '<%=request.getContextPath()%>/warehouse/saveWareHouseClassJson.action'; 
	    //$("#wc\\.parent\\.id").val(node.attributes.parentId);
   }  
   // 新增下级
   function newLowPc(){
	   closeTr();
   		var node = $('#wcTree').tree('getSelected');
   		if(node==null){
   			$.messager.alert("提示信息","请选择分类!");
   			return;
   		}
   		currentParentId  = node.id;
   		var title = "新增【" + node.text + "】下的仓库分类";
   		$('#dlg').dialog('open').dialog('setTitle',title);  
	    $('#fm').form('clear');  
	    url = '<%=request.getContextPath()%>/warehouse/saveLowWareHouseClassJson.action'; 
	    $("#wc\\.parent\\.id").val(node.id);
   }
   function editPc(){
	   openTr();
   		var node = $("#wcTree").tree('getSelected');
   		var title = "修改仓库分类【" + node.text + "】";
   		$('#dlg').dialog('open').dialog('setTitle',title);
   		$("#wc\\.id").val(node.id);
   		if(typeof(node.attributes.parentId) != "undefined"){
   			currentParentId  = node.attributes.parentId;
   		}else{
   			currentParentId = "";
   		}
   		$("#wc\\.name").val(node.text);
   		$("#wc\\.description").val(node.attributes.description);
   		if(node.attributes.parentId!=null){
   			$("#wc\\.parent\\.id").val(node.attributes.parentId);
   			$("#parentName").val(node.attributes.parentName);
   		}else{
   			$("#parentName").val("无");
   		}
	    url = '<%=request.getContextPath()%>/warehouse/updateWareHouseClassJson.action'; 
   }
   function deletePc(){
   		var node = $('#wcTree').tree('getSelected');
   		$("#wc\\.parent\\.id").val(node.attributes.parentId);
   		wareHouseClassService.valDelWHClass(node.id,function(data){
   			if(node==null){
	   			$.messager.alert("提示信息","请选择分类!");
	   			return;
		   	}
	   		if(data == "1"){
	   			$.messager.alert("提示信息","该分类下已有分类，请先删除!",'warning');
	   			return;
	   		}
	   		if(data == "2"){
	   			$.messager.alert("提示信息","该分类下已有仓库信息，请先删除!",'warning');
	   			return;
	   		}
	   		$.messager.confirm('删除仓库分类', '确认删除【' + node.text + '】吗?', function(r){
				if (r){
					 $.ajax({
					 	type:"post",
					 	data:"wc.id=" + node.id, 
					 	url:"<%=request.getContextPath()%>/warehouse/delWareHouseClassJson.action",
						success:function(result){
						   var data = eval('('+result+')');
				           reloadMenu();				           
				           var msg = "【" + data.text;
					       msg = msg + "】删除成功!";
			               $.messager.show({title:"提示信息",msg:msg,showType:'slide'});
			               $('#dlg').dialog('close');      // close the dialog  
						}
					 })
				}
			});
   		})
   }
   function savePC(){  
	    $('#fm').form('submit',{  
	        url: url,  
	        onSubmit: function(){  
	        	if($(this).form('validate')){
	        		$("#saveBtn").linkbutton("disable");
	        	}
	            return $(this).form('validate');  
	        },  
	        success: function(result){  
	        	$("#saveBtn").linkbutton("enable");
	       		var result = eval('('+result+')');
	        	var msg = "【" + result.text ;
	        	if($("#wc\\.id").val()!=""){
	        		msg = msg + "】修改成功!";
	        	}else{
	        		msg = msg + "】新增成功!";
	        	}
            	$.messager.show({title:"提示信息",msg:msg,showType:'slide'});
                $('#dlg').dialog('close');      // close the dialog  
                
             	// 刷新当前修改完的父级分类
              var node = $('#wcTree').tree('getSelected');
              	reloadMenu();
                var parentPcId = $("#wc\\.parent\\.id").val();
              	if(currentParentId != parentPcId){
              		$('#wcTree').tree('remove',node.target); // 不删除的话原先已更改分类的分类还会留着
              	}
	        }  
	    });  
	} 
	// 增删改完毕重读菜单
	function reloadMenu(){
		// 重读该分类
      	var expandId = $("#wc\\.parent\\.id").val();
       	// expandId为0表明是最父级分类
      	if(expandId != 0 && expandId != ""){
      		var node = $('#wcTree').tree('find', expandId);
      		$('#wcTree').tree('reload',node.target);
       	}else{
       		$('#wcTree').tree({url:getClassUrl});
       		$('#wcTree').tree('reload');
       	}
	}
	function reloadPc(){
		var node = $('#wcTree').tree('getSelected');
		if(node==null){
			location.reload();
		}else{
			$('#wcTree').tree('reload',node.target);
		}
	} 
	
	function openSelectWC(){
		$('#spcTree').tree({
			animate: true,
			url: '<%=request.getContextPath()%>/warehouse/getAllWareHouseClassJson.action',
			onBeforeExpand:function(node){
				$('#spcTree').tree("options").url="<%=request.getContextPath()%>/warehouse/getAllWareHouseClassJson.action?wc.id=" + node.id;
			},
			onClick:function(node){
				$('#spcTree').tree('expand',node.target);
			},
			onContextMenu: function(e, node){
				e.preventDefault();
				$('#spcTree').tree('select', node.target);
				$('#fm').menu('show', {
					left: e.pageX,
					top: e.pageY
				});
			}
		});
   		var title = "仓库分类选择";
   		$('#pcDlg').dialog('open').dialog('setTitle',title);  
	}
	function selectPc(){
		var parentNode = $('#spcTree').tree('getSelected'); // 所选择的分类
		var node = $('#wcTree').tree('getSelected'); // 当前操作的分类
		parentTreePath = parentNode.attributes.treepath;
		nodeTreePath = node.attributes.treepath;
		if(parentNode.id == node.id){
			$.messager.alert("提示信息","不能关联当前分类为上级分类!","warning");
			return;
		}
		if(parentTreePath.indexOf(nodeTreePath) != "-1"){
			$.messager.alert("提示信息","不能关联当前分类的子类为上级分类!","warning");
			return;
		}
		$("#parentName").val(parentNode.text);
		$("#wc\\.parent\\.id").val(parentNode.id);
		$('#pcDlg').dialog('close');
	}
	function setFirstClass(){
		$("#wc\\.parent\\.id").val(null);
   		$("#parentName").val("无");
	}
	function closeTr(){
   		$("#tr1").hide();
   		$("#tr2").hide();
   }
   function openTr(){
   		$("#tr1").show();
   		$("#tr2").show();
   }
</script>
</head>
<body class="easyui-layout">
<div region="north" title="仓库类别管理" border="true" style="height:59px;">
<div id="tb" class="datagrid-toolbar">  
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:newSamePc()">新增一级</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-lowadd" plain="true" onclick="javascript:newLowPc()">新增下级</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="javascript:editPc()">修改</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:deletePc()">删除</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="javascript:reloadPc()">刷新</a> 
</div>  
</div>
<div region="center" title="仓库类别信息" >
<ul id="wcTree"></ul>
</div>
<div id="dlg" class="easyui-dialog" style="width:290px;height:230px"  
        closed="true" buttons="#wc-dlg-buttons"> 
    <form id="fm" method="post">  
    	<table>
    		<tr>
        		<td>名称:</td>  
        		<td><input id="wc.name" name="wc.name" class="easyui-validatebox" required="true" maxlength="50" /></td>
	            <input id="wc.parent.id" name="wc.parent.id" type="hidden" value="" />
            	<input id="wc.id" name="wc.id" type="hidden" value="" />  
	        </tr>
	        <tr style='display:none;' id="tr1">
            	<td>上级分类:</td>
            	<td>
            		<input type="text" name="parentName" id="parentName" class="easyui-validatebox"   readonly="readonly" style="PADDING-LEFT: 20px;BACKGROUND-IMAGE: url(<%=request.getContextPath()%>/images/note_add.png); BACKGROUND-REPEAT: no-repeat;cursor:pointer;"  onclick="openSelectWC()" size="17" />
            	</td>
            </tr> 
            <tr style='display:none;' id="tr2">
            	<td>
            		&nbsp;
            	</td>
            	<td>
            		<a href="#" onclick="setFirstClass()">设为一级</a>
            	</td>
            </tr>
            <tr>
	            <td>描述:</td>
	            <td>
	            <textarea id="wc.description" name="wc.description" class="easyui-validatebox" maxlength="200" rows="3"></textarea>
	            </td> 
            </tr>
    	</table>  
    </form>  
</div>
<div id="wc-dlg-buttons">  
    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="savePC()" id="saveBtn">保存</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>  
</div> 
<!-- 新增修改弹出框 -->
<div id="pcDlg" class="easyui-dialog" style="width:400px;height:380px" closed="true" buttons="#dlg-buttons"> 
    <div class="easyui-layout" fit="true" style="background:#ccc;">
		<div region="center" style="overflow:hidden;">
			<ul id="spcTree"></ul>
		</div> 
	</div>
	<div id="dlg-buttons">  
	    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="selectPc()">选择</a>  
	    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#pcDlg').dialog('close')">关闭</a>  
	</div> 
</div>
</body>
</html>